const App = {
    mixins: [Save],//配置文件，路径/utils/mixins.js
    data() {
        return {
            dataForm2: {
            },
            dataForm1: {},
            dataForm3: [],//自建房时，查询全部的房间进行回写
            dataForm4: {},
            Grid: [],
            house_type: [],
            blue_plate: [],
            personal: [],
            relation: [],
            childDataForm1: [{}],
            Blue_Plate_SerialNumber: '',
            House_Room_SerialNumber: '',
        };
    },
    mounted() {
        let ls_URL = window.location.href;
        this.Blue_Plate_SerialNumber = getQueryObject(ls_URL, "Blue_Plate_SerialNumber");// 获取 nodeKey 参数
        this.House_Room_SerialNumber = getQueryObject(ls_URL, "House_Room_SerialNumber");   // 获取 action 参数
        this.getCategory();
        this.getData();
    },
    methods: {
        getData() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'blue_plate_information',
                    Condition: "Row_Status='0' AND Blue_Plate_SerialNumber='" + this.Blue_Plate_SerialNumber + "'",
                },
                success: (data) => {
                    this.dataForm2 = data[0];
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            if (this.House_Room_SerialNumber) {
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    type: "POST",
                    data: {
                        Data: 'blue_plate_house_room',
                        Condition: "House_Room_SerialNumber='" + this.House_Room_SerialNumber + "'",
                    },
                    success: (data) => {
                        this.dataForm1 = data[0];
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });
                if (this.action == 'Modify') {
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                        type: "POST",
                        data: {
                            Data: 'blue_plate_person',
                            Condition: "House_Room_SerialNumber='" + this.House_Room_SerialNumber + "'",
                        },
                        success: (data) => {
                            this.childDataForm = data;
                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json",
                    });
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                        type: "POST",
                        data: {
                            Data: 'blue_plate_house_holder',
                            Condition: "House_Room_SerialNumber='" + this.House_Room_SerialNumber + "' ORDER BY Create_Datetime DESC LIMIT 1",
                        },
                        success: (data) => {
                            if (data.length == 1) {
                                this.childDataForm1 = data;
                            }
                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json",
                    });
                }
            } else {
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    type: "POST",
                    data: {
                        Data: 'blue_plate_self_built_house',
                        Condition: "Blue_Plate_SerialNumber='" + this.Blue_Plate_SerialNumber + "'",
                    },
                    success: (data) => {
                        this.dataForm4 = data[0];
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    type: "POST",
                    data: {
                        Data: 'blue_plate_house_room',
                        Condition: "Blue_Plate_SerialNumber='" + this.Blue_Plate_SerialNumber + "'",
                    },
                    success: (data) => {
                        this.dataForm3 = data;
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });
                if (this.action == 'Modify') {
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                        type: "POST",
                        data: {
                            Data: 'blue_plate_person',
                            Condition: "Blue_Plate_SerialNumber='" + this.Blue_Plate_SerialNumber + "'",
                        },
                        success: (data) => {
                            this.childDataForm = data;
                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json",
                    });
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                        type: "POST",
                        data: {
                            Data: 'blue_plate_house_holder',
                            Condition: "Blue_Plate_SerialNumber='" + this.Blue_Plate_SerialNumber + "' ORDER BY Create_Datetime DESC LIMIT 1",
                        },
                        success: (data) => {
                            if (data.length == 1) {
                                this.childDataForm1 = data;
                            }
                        },
                        complete: (XMLHttpRequest) => {
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json",
                    });
                }
            }
        },
        getCategory() {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'blue_plate_information',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.blue_plate = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'grid_information',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.Grid = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'personal_information',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.personal = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'blue_plate_house_type',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.house_type = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryByTableName`,
                type: "POST",
                data: {
                    Data: 'relation_type',
                    Condition: "Row_Status='0'",
                },
                success: (data) => {
                    this.relation = data; // 直接使用 this
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json",
            });

        },
        handleChange(val, item) {
            if (val == 'RT0000000000010') {
                item.Is_Owner = '0'
            } else {
                item.Is_Owner = '1'
            }

        },
        handleHolder(val, item) {
            item.Ownership_Prove = ''
            let obj = this.childDataForm.filter(i => i.Relation_Type_SerialNumber == 'RT0000000000010')[0];
            if (obj) {
                obj.Relation_Type_SerialNumber = ''
            }
            let obj1 = this.childDataForm.filter(i => i.ID_Number == val)[0];
            if (obj1) {
                obj1.Relation_Type_SerialNumber = 'RT0000000000010'
            }
        },
        handleIDNumber(val, item) {
            item.Relation_Type_SerialNumber = ''
            if (this.childDataForm1[0].ID_Number == val) {
                item.Relation_Type_SerialNumber = 'RT0000000000010'
            }
        },
        skip() {
            window.location.href = '../../personal_information/personalAdd.html?action=Add&Personal_Resource=2'
        },
        async handleSubmit() {
            const allValid = await this.validateAllForms();
            if (allValid) {
                //1.获取全部的
                let lJSONObject_DataSet = new Object();
                if (this.House_Room_SerialNumber) {//常规
                    this.childDataForm1[0].House_Room_SerialNumber = this.dataForm1.House_Room_SerialNumber;
                    this.childDataForm1[0].Blue_Plate_SerialNumber = this.dataForm2.Blue_Plate_SerialNumber;
                    if (this.action == 'Add') {
                        this.childDataForm.forEach(item => {
                            item.Blue_Plate_SerialNumber = this.Blue_Plate_SerialNumber;
                            item.House_Room_SerialNumber = this.House_Room_SerialNumber;
                        })
                        this.dataForm1.Live_Status = '1';
                        lJSONObject_DataSet = {
                            Add: {
                                blue_plate_person: this.childDataForm, blue_plate_house_holder: this.childDataForm1
                            },
                            Modify: {
                                blue_plate_house_room: [this.dataForm1]
                            }
                        }
                    } else if (this.action == 'Modify') {
                        const { add, modify } = this.deal(this.childDataForm);
                        let blue_plate_house_holder = [], blue_plate_house_holder1 = [];
                        if (this.childDataForm1[0].ID_Number != this.childDataForm1[0].ID_Number_Original) {
                            blue_plate_house_holder.push({
                                House_Room_SerialNumber: this.childDataForm1[0].House_Room_SerialNumber,
                                ID_Number: this.childDataForm1[0].ID_Number,
                                Ownership_Prove: this.childDataForm1[0].Ownership_Prove
                            })
                        } else if (this.childDataForm1[0].ID_Number == this.childDataForm1[0].ID_Number_Original && this.childDataForm1[0].Ownership_Prove != this.childDataForm1[0].Ownership_Prove_Original) {
                            blue_plate_house_holder1 = this.childDataForm1;
                        }
                        if (add == null) {
                            lJSONObject_DataSet = {
                                Modify: {
                                    blue_plate_person: modify, blue_plate_house_holder: blue_plate_house_holder1
                                },
                                Add: {
                                    blue_plate_house_holder
                                }
                            }
                        } else {
                            lJSONObject_DataSet = {
                                Add: {
                                    blue_plate_person: add, blue_plate_house_holder
                                },
                                Modify: {
                                    blue_plate_person: modify
                                }
                            }
                        }
                    }
                } else {  //自建房
                    this.childDataForm1[0].Blue_Plate_SerialNumber = this.dataForm2.Blue_Plate_SerialNumber;
                    if (this.action == 'Add') {
                        this.childDataForm.forEach(item => {
                            item.Blue_Plate_SerialNumber = this.Blue_Plate_SerialNumber;
                        })
                        this.dataForm3.forEach(item => {
                            item.Live_Status = '1'
                        })
                        this.dataForm4.ID_Number = this.childDataForm1[0].ID_Number;
                        let obj = this.personal.filter(i => i.ID_Card_Number == this.childDataForm1[0].ID_Number)[0];
                        this.dataForm4.Contact_Phone = obj.Contact_Phone;
                        lJSONObject_DataSet = {
                            Add: {
                                blue_plate_person: this.childDataForm, blue_plate_house_holder: this.childDataForm1
                            },
                            Modify: {
                                blue_plate_house_room: this.dataForm3, blue_plate_self_built_house: [this.dataForm4]
                            }
                        }
                    } else if (this.action == 'Modify') {
                        const { add, modify } = this.deal(this.childDataForm);
                        let blue_plate_house_holder = [], blue_plate_house_holder1 = [];
                        if (this.childDataForm1[0].ID_Number != this.childDataForm1[0].ID_Number_Original) {
                            blue_plate_house_holder.push({
                                House_Room_SerialNumber: this.childDataForm1[0].House_Room_SerialNumber,
                                ID_Number: this.childDataForm1[0].ID_Number,
                                Ownership_Prove: this.childDataForm1[0].Ownership_Prove
                            })
                        } else if (this.childDataForm1[0].ID_Number == this.childDataForm1[0].ID_Number_Original && this.childDataForm1[0].Ownership_Prove != this.childDataForm1[0].Ownership_Prove_Original) {
                            blue_plate_house_holder1 = this.childDataForm1;
                        }
                        this.dataForm4.ID_Number = this.childDataForm1[0].ID_Number;
                        let obj = this.personal.filter(i => i.ID_Card_Number == this.childDataForm1[0].ID_Number)[0];
                        this.dataForm4.Contact_Phone = obj.Contact_Phone;
                        if (add == null) {
                            lJSONObject_DataSet = {
                                Modify: {
                                    blue_plate_person: modify, blue_plate_house_holder: blue_plate_house_holder1, blue_plate_self_built_house: [this.dataForm4]
                                },
                                Add: {
                                    blue_plate_house_holder
                                }
                            }
                        } else {
                            lJSONObject_DataSet = {
                                Add: {
                                    blue_plate_person: add, blue_plate_house_holder
                                },
                                Modify: {
                                    blue_plate_person: modify, blue_plate_self_built_house: [this.dataForm4]
                                }
                            }
                        }
                    }
                }
                let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                if (!lJSONObject_DataSet['Add']) {
                    lJSONObject_DataSet['Add'] = new Object();
                }
                lJSONObject_DataSet['Add']['base_operation_log'] = [{
                    Operation_Type: '0',
                    Operation_Path: this.relativePath,
                    Operation_Main,
                    Operation_Interface: "generalSave",
                    Operation_IP: getIpAddress()
                }]
                submitSave(lJSONObject_DataSet, (res) => {
                    if (res.Status == 'success') {
                        window.location.href = './index.html'
                    }
                })
                // $.ajax({
                //     headers: requestHeaders(),
                //     url: `${mh}/generalBusiness/generalSave`,
                //     method: "post",
                //     data: {
                //         Data: JSON.stringify(lJSONObject_DataSet),
                //     },
                //     success: (res) => {
                //         if (res.Status == 'success') {
                //             window.location.href = './index.html'
                //         }
                //     },
                //     complete: (XMLHttpRequest) => {
                //         requestPretreatment(XMLHttpRequest);
                //     },
                // });
            }
        },
        deal(arr) {    //将待提交的数据中添加和修改分离出来
            const add = [], modify = [];
            for (const i in arr) {
                const obj = arr[i];
                if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                    add.push(obj);
                } else {
                    modify.push(obj);
                }
            }
            if (add.length == 0) {
                return { add: null, modify };
            }
            return { add, modify };

        },
        handleAdd(formName) {
            if (this.action === 'Add') {
                this[formName].push({})
            } else if (this.action === 'Modify') {
                let lJSONObject_Data_Options = new Object();
                lJSONObject_Data_Options = JSON.parse(document.getElementById("MainTable").getAttribute("data-options"));
                let ls_primary_key = lJSONObject_Data_Options.primary_key;
                let lJSONObject_Data = new Object();
                lJSONObject_Data[ls_primary_key] = this.Blue_Plate_SerialNumber;
                lJSONObject_Data['House_Room_SerialNumber'] = this.House_Room_SerialNumber;
                this[formName].push(lJSONObject_Data);
            }
        },
    },
};
const { createApp, ref } = Vue;
const app = createApp(App);
app.use(ElementPlus, { locale: zhCn });
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
app.component('yl-upload', ylUpload);
app.component('yl-text-editor', ylTextEditor);
app.component('yl-select', ylSelect)
const ElMessage = ElementPlus.ElMessage;
app.mount("#app");
